﻿using Microsoft.EntityFrameworkCore;
using PMS.Domain.AggregatesModel.ProjectAggregate;
using PMS.Domain.Seedwork;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace PMS.Infrastructure.Repositories
{
    public class ProjectRepository : IProjectRepository
    {
        private readonly PMSContext _dbContext;
        public ProjectRepository(PMSContext projectContext)
        {
            _dbContext = projectContext;
        }
        public IUnitOfWork UnitOfWork => _dbContext;


        public Project Add(Project project)
        {
            if (project.IsTransient())
            {
                return _dbContext.Add(project).Entity;
            }
            return project;
        }     

        public async Task<Project> GetAsync(int id)
        {
            var project = await _dbContext.Projects
                        .Include(p => p.Contributors)                          
                        .Include(p =>  p.ProjectConsumablies)
                        .Include(p =>p.AuditAdvicies)                    
                        .SingleOrDefaultAsync(b => b.Id == id);
            return project;
        }

        public void Update(Project project)
        {
            _dbContext.Projects.Update(project);
        }

     
    }
}
